/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.cadastro;

import br.com.saber.certificacao.beansjsf.interfacebean.BeanJSFInterface;
import br.com.saber.certificacao.beansjsf.login.LoginManager;
import br.com.saber.certificacao.beansjsf.semescopo.SelectItemManager;
import br.com.saber.certificacao.ejbs.stateless.interfaces.VersaoBeanLocal;
import br.com.saber.certificacao.modelos.Usuario;
import br.com.saber.certificacao.modelos.Versao;
import br.com.saber.certificacao.modelos.validadores.exceptions.generica.ValidacaoExcecao;
import br.com.saber.certificacao.observador.ControleObserver;
import br.com.saber.certificacao.utils.UtilMetodos;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author gbvbahia
 */
@ManagedBean(name = "cadastroVersao")
@ViewScoped
public class VersaoManager implements BeanJSFInterface {

    @EJB
    private VersaoBeanLocal versaoBean;

    private Versao versaoCadastro;

    private List<Versao> modelVersoes;

    private Locale locale = new Locale("pt, BR");

    @ManagedProperty("#{loginManager}")
    private LoginManager loginManager;

    @ManagedProperty("#{selectItemManager}")
    private SelectItemManager selectItemManager;

    /** Creates a new instance of VersaoManager */
    public VersaoManager() {
    }

    //====================
    // Iniciadores
    //====================
    @Override
    @PostConstruct
    public void init() {
        versaoCadastro = new Versao();
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "VersaoManager.init() executado!");
    }

    @Override
    @PreDestroy
    public void end() {
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "VersaoManager.end() executado!");
    }

    //====================
    //Métodos de Negócio
    //====================
    public void salvarVersao(ActionEvent event) {
        try {
            List<Usuario> usuarios = new ArrayList<Usuario>();
            usuarios.add(loginManager.getUsuario());
            if(versaoCadastro.isMother())versaoCadastro.setVersaoMother(null);
            versaoBean.salvarVersao(versaoCadastro, usuarios);
            init();
            ControleObserver.notificaObservers(loginManager.getUsuario(), new String[]{ControleObserver.Eventos.CAD_VERSAO_EVENT});
            UtilMetodos.messageFactoringFull("versaoCadastradaOK", FacesMessage.SEVERITY_INFO, FacesContext.getCurrentInstance());
        } catch (ValidacaoExcecao e) {
            if (!StringUtils.isBlank(e.getAtributoName())) {
                UtilMetodos.messageFactoringFull(UtilMetodos.getResourceBundle(e.getMessage(), FacesContext.getCurrentInstance()), null, e.getAtributoName(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            } else {
                UtilMetodos.messageFactoringFull(e.getMessage(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            }
        }
        atualizarVersoes();
    }

    public void atualizarVersoes() {
        this.modelVersoes = new ArrayList(versaoBean.buscarTodasVersoes());
    }

    //====================
    //Table Actions
    //====================

    //====================
    //SelectItem
    //====================
    public List<SelectItem> getCertificacoes() {
        return this.selectItemManager.getCertificacoes(this.loginManager.getUsuario());
    }
    
    public List<SelectItem> getVersoesMother() {
        if(!this.versaoCadastro.isMother() && versaoCadastro.getCertificacao() != null)
        return this.selectItemManager.getVersaoPorTipo(true,versaoCadastro.getCertificacao());
        return new ArrayList<SelectItem>();
    }

    //=========================
    //Getters AND Setters
    //=========================
    public LoginManager getLoginManager() {
        return loginManager;
    }

    public void setLoginManager(LoginManager loginManager) {
        this.loginManager = loginManager;
    }

    public SelectItemManager getSelectItemManager() {
        return selectItemManager;
    }

    public void setSelectItemManager(SelectItemManager selectItemManager) {
        this.selectItemManager = selectItemManager;
    }

    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public List<Versao> getModelVersoes() {
        if (this.modelVersoes == null) {
            atualizarVersoes();
        }
        return modelVersoes;
    }

    public void setModelVersoes(List<Versao> modelVersoes) {
        this.modelVersoes = modelVersoes;
    }

    public Versao getVersaoCadastro() {
        return versaoCadastro;
    }

    public void setVersaoCadastro(Versao versaoCadastro) {
        this.versaoCadastro = versaoCadastro;
    }
}
